<?php

class UserAction extends BaseAction {

    public function index() {
        $ye = M("users");
        $money = $ye->where("name = '" . $_SESSION['name'] . "'")->getField("money"); //当前余额
        $this->assign("money", $money);
        $notice = M("notice");
        $nlist = $notice->where("str=1")->order("sort asc,ftime desc")->limit("0,5")->select();
        $this->assign("nlist", $nlist);
        $this->templist(5);
       	$this->orderlist1();
        $this->display();
    }

    public function update_do() {
        $user = M("Users");
        if ($user->create()) {
            unset($user->phone);
            if ($user->qq == "未填写")
                $user->qq = "";
            $user->id = $_SESSION['userid'];
            $name = $user->name;
            $qq = $user->qq;
            $result = $user->save();
            if ($result) {
                $_SESSION['name'] = $name;
                $_SESSION['qq'] = $qq;
                $this->success("保存成功");
            } else {
                $this->error("未修改");
            }
        } else {
            $this->error("系统出错");
        }
    }

    public function temps($up = "") {
        $this->assign("up", $up);
        $this->templist();
        $this->display();
    }

    public function temp_admin() {
        $action = $this->_param("action");
        $rate = $this->_param("rate");
        $id = $this->_param("id");
        if ($action == "del") {
            $this->deltemp($id);
        }
        if ($action == "up") {
            $this->changestatus(0, $id);
        }
        if ($action == "down") {
            $this->changestatus(1, $id);
        }
        if ($action == "rate") {
            $tempModel = M("Temps");
            $map['id'] = array('in', $id);
            $result = $tempModel->where($map)->setField('rate', $rate);
            if ($result) {
                $this->redirect("/User/temps", 0);
            }
        }
    }

    public function deltemp($id) {
        $id = $this->_param("id");
        $tempModel = M("Temps");
        if (!is_array($id))
            $id = array($id);
        $map['id'] = array('in', $id);
        $result = $tempModel->where($map)->delete();
        if ($result) {
            $this->redirect("/User/temps", 0);
        }
    }

    public function changestatus($s, $id) {
        $tempModel = M("Temps");
        $status = $s == 0 ? 1 : 0;
        if (is_array($id)) { //多个提交
            foreach ($id as $key => $vid) {
                $li[$key] = $tempModel->where("id = $vid")->order("id desc")->select();
                if ($li[$key][0]['trade'] != 0 && $li[$key][0]['color'] != 0 && $li[$key][0]['sty'] != 0 && $li[$key][0]['num'] != 0) {
                    $id1[$key] = $vid;
                }
            }
            if ($s == 0) {
                $map['id'] = array('in', $id1);
            } else {
                $map['id'] = array('in', $id);
            }
            if ($status == 1) {
                $data = array('status' => $status, 'ftime' => date("Y-m-d H:i:s")); //上架状态和上架时间
            } else {
                $data = array('status' => $status); //下架状态
            }
            $result = $tempModel->where($map)->setField($data);
            if ($result) {
                if ($s == 0) {
                    $this->redirect("/User/temps/up/1", 0);
                } else {
                    $this->redirect("/User/temps", 0);
                }
            }
        } else { //单个提交
            $map['id'] = array('in', $id);
            $li = $tempModel->where("id = $id")->order("id desc")->select();
            if ($li[0]['farmwork'] == 0 || $li[0]['trade'] == 0 || $li[0]['color' == 0] || $li[0]['sty'] == 0 || $li[0]['num'] == 0) { //信息不完善
                echo "0";
            } else {
                if ($status == 1) {
                    $data = array('status' => $status, 'ftime' => date("Y-m-d H:i:s")); //上架状态和上架时间
                } else {
                    $data = array('status' => $status); //下架状态
                }
                $result = $tempModel->where($map)->setField($data);
                echo "1";
            }
        }
    }
	
	//@$num 分类时，每一页显示的数量
    public function templist($num = 25) {
        $tempModel = M("Temps");
        import('ORG.Util.Page'); // 导入分页类
		$map['userid']=$_SESSION['userid'];
        $temps = $tempModel->where($map)->order("status desc,rate desc,type asc")->select();
        $count = count($temps); // 查询满足要求的总记录数
        $Page = new Page($count, $num);
        $show = $Page->show(); // 分页显示输出
        $list = $tempModel->where($map)->order('status desc,rate desc,type asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('templist', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出			
    }

    public function changepwd_do() {
        $user = M("Users");
        $id = $_SESSION['userid'];
        $old_pwd = $this->_param("old_pwd");
        $new_pwd = $this->_param("new_pwd");
        $cfm_new_pwd = $this->_param("cfm_new_pwd");
        $result = $user->select($id);
        if (md5($old_pwd) != $result[0]['pwd']) {
            $this->error("原始密码有误");
        } elseif ($new_pwd != $cfm_new_pwd) {
            $this->error("两次密码输入不一致");
        }
        $data['id'] = $id;
        $data['pwd'] = md5($new_pwd);
        $result = $user->save($data);
        if ($result) {
            $this->success("修改成功");
        } else {
            $this->error("未修改");
        }
    }

    public function chongzhi() {
        $this->chongzhijilu();
        $this->display();
    }

    public function chongzhijilu() {
        $chongModel = M("Chongzhi");
        $chongzhijilu = $chongModel->where("userid=" . $_SESSION['userid'])->select();
        $this->assign("chongzhijilu", $chongzhijilu);
    }

    public function chongzhi_do() {
        header("Content-type:text/html;charset=utf-8");
        $notify_url = "http://" . $_SERVER['HTTP_HOST'] . "/return_url.php";
        $return_url = "http://" . $_SERVER['HTTP_HOST'] . "/return_url.php";
        $out_trade_no = date("YmdHis") . $_SESSION['userid']; //订单号			
        $subject = "模板返利网现金充值"; //工具名称
        $total_fee = $this->_param("money"); //价格	
        $total_fee = 0.01;
        $body = "模板返利网现金充值，充值金额：{$total_fee}元。祝您模板热销！！！";
        $this->alipay($out_trade_no, $subject, $body, $total_fee, $return_url, $notify_url);
    }

    public function chongzhi_ok() {
        $out_trade_no = $this->_param('out_trade_no'); //获取订单号
        $trade_no = $this->_param('trade_no');  //获取支付宝交易号
        $total_fee = $this->_param('total_fee');  //获取总价格
        $user = M("Users");
        $user->where('id=' . $_SESSION['userid'])->setInc('money', $total_fee);
        $chongModel = M("Chongzhi");
        $data['id'] = $out_trade_no;
        $data['taoid'] = $trade_no;
        $data['money'] = $total_fee;
        $data['userid'] = $_SESSION['userid'];
        $data['date'] = date("YmdHis");
        $result = $chongModel->add($data);
        if (!$result) {
            $this->assign("err", 1);
        }
        $this->chongzhijilu();
        $this->assign("data", $data);
        $this->display();
    }

    public function tixian() {
        $_SESSION['tixian_verify'] = md5(date("YmdHis") . rand(0, 9999));
        $this->tixianjilu();
        $this->display();
    }

    public function tixian_apply() {
        if (!isset($_SESSION['tixian_verify'])) {
            $this->tixianjilu();
            $this->display();
            exit();
        }
        $tixianModel = M("Tixian");
        if ($tixianModel->create()) {
            $tixianModel->userid = $_SESSION['userid'];
            $tixianModel->date = date("YmdHis");
            $result = $tixianModel->add();
            $tixian = $tixianModel->select($result);
            $_SESSION['tixian_info'] = $tixian[0];
            if ($result) {
                unset($_SESSION['tixian_verify']);
                $this->tixianjilu();
                $this->display();
            } else {
                $this->error("系统有点忙，请稍候再试一次吧，哈哈~");
            }
        } else {
            $this->error("系统出错");
        }
    }

    public function tixianjilu() {
        $tixianModel = M("Tixian");
        $tixianjilu = $tixianModel->where("userid=" . $_SESSION['userid'])->order("date desc")->select();
        $this->assign("tixianjilu", $tixianjilu);
    }

    public function alipay($out_trade_no, $subject, $body, $total_fee, $return_url, $notify_url) {
        require_once("alipay_service.class.php");
        $aliapy_config['partner'] = '2088102005217257';
        $aliapy_config['key'] = '7twoj83o6qkxgmoiw7p6oj4cz5g4uyat';
        $aliapy_config['seller_email'] = 'noworld_008@163.com';
        $aliapy_config['return_url'] = $return_url;
        $aliapy_config['notify_url'] = $notify_url;
        $aliapy_config['sign_type'] = 'MD5';
        $aliapy_config['input_charset'] = 'utf-8';
        $aliapy_config['transport'] = 'http';
        $paymethod = '';
        $defaultbank = '';
        $anti_phishing_key = '';
        $exter_invoke_ip = '';
        $show_url = 'http://' . $_SERVER['HTTP_HOST'];
        $extra_common_param = '';
        $royalty_type = "";   //提成类型，该值为固定值：10，不需要修改
        $royalty_parameters = "";
        $parameter = array(
            "service" => "create_direct_pay_by_user",
            "payment_type" => "1",
            "partner" => trim($aliapy_config['partner']),
            "_input_charset" => trim(strtolower($aliapy_config['input_charset'])),
            "seller_email" => trim($aliapy_config['seller_email']),
            "return_url" => trim($aliapy_config['return_url']),
            "notify_url" => trim($aliapy_config['notify_url']),
            "out_trade_no" => $out_trade_no,
            "subject" => $subject,
            "body" => $body,
            "total_fee" => $total_fee,
            "paymethod" => $paymethod,
            "defaultbank" => $defaultbank,
            "anti_phishing_key" => $anti_phishing_key,
            "exter_invoke_ip" => $exter_invoke_ip,
            "show_url" => $show_url,
            "extra_common_param" => $extra_common_param,
            "royalty_type" => $royalty_type,
            "royalty_parameters" => $royalty_parameters,
        );
        $alipayService = new AlipayService($aliapy_config);
        $html_text = $alipayService->create_direct_pay_by_user($parameter);
        echo $html_text;
    }

    public function gettemps() {
        header("Content-type:text/html;charset=utf-8");
        set_time_limit(0);
        $wangwang = iconv("utf-8", "gbk", $this->_param("designer"));
        $search_designer_url = "http://zxn.taobao.com/designer_list.htm?q=" . urlencode($wangwang);
        $search_designer_content = file_get_contents($search_designer_url);
        preg_match("/<a\s*class=\"logo\"\s*target=\"[^\"]+\"\s+href=\"(.+)\"/", $search_designer_content, $matches); //根据绑定的淘宝账号，得到设计师主页面地址
        $designer_home_url = $matches[1];
        $designer_home_url = str_replace("http://zx.", "http://zxn.", $designer_home_url); //将设计师主页强制转换为新旺铺模板列表
        //dump($designer_home_url);
        $designer_home_content = file_get_contents($designer_home_url);
        preg_match("/共(\d+)页/", iconv("gbk", "utf-8", $designer_home_content), $pagematch); //在设计师主页中搜索模板列表页面总数
        $pageCount = $pagematch[1];
        preg_match_all("/http:\/\/zxn.taobao.com\/detail.htm?[^\"]+/", $designer_home_content, $tempurlmatches);  //将设计师模板列表第一页中所有的模板URL地址提取出来
        $newTempsUrl = $tempurlmatches[0];
        //dump($newTempsUrl);	
        if ($pageCount > 1) {  //如果总该设计师新模板列表多于1页，半再将其它页面的模板URL和模板缩略图提取出来，并将所有的模板URL和缩略图合并成一个数组
            for ($i = 2; $i <= $pageCount; $i++) {
                $designer_home_p_url = $designer_home_url . "&p=" . $i;
                $designer_home_p_content = file_get_contents($designer_home_p_url);
                preg_match_all("/http:\/\/zxn.taobao.com\/detail.htm?[^\"]+/", $designer_home_p_content, $tempurlmatchesp);
                $newTempsUrl = array_merge($newTempsUrl, $tempurlmatchesp[0]);
                //dump($tempurlmatchesp[0]);	
            }
        }
        $designer_old_home_url = str_replace("http://zxn.", "http://zx.", $designer_home_url);
        $designer_old_home_content = file_get_contents($designer_old_home_url);
        preg_match("/共(\d+)页/", iconv("gbk", "utf-8", $designer_old_home_content), $oldpagematch); //在设计师主页中搜索模板列表页面总数
        $oldpageCount = $oldpagematch[1];

        preg_match_all("/http:\/\/zx.taobao.com\/detail.htm?[^\"]+/", $designer_old_home_content, $oldtempurlmatches);  //将设计师模板列表第一页中所有的模板URL地址提取出来
        $oldTempsUrl = $oldtempurlmatches[0];
        //dump($oldTempsUrl);

        if ($oldpageCount > 1) {  //如果总该设计师新模板列表多于1页，半再将其它页面的模板URL和模板缩略图提取出来，并将所有的模板URL和缩略图合并成一个数组
            for ($i = 2; $i <= $oldpageCount; $i++) {
                $designer_old_home_p_url = $designer_old_home_url . "&p=" . $i;
                $designer_old_home_p_content = file_get_contents($designer_old_home_p_url);
                preg_match_all("/http:\/\/zx.taobao.com\/detail.htm?[^\"]+/", $designer_old_home_p_content, $oldtempurlmatchesp);
                $oldTempsUrl = array_merge($oldTempsUrl, $oldtempurlmatchesp[0]);
                //dump($oldtempurlmatchesp[0]);
            }
        }
        $tempsUrl = array_merge($newTempsUrl, $oldTempsUrl);
        $flag = 0;
        $tempModel = M("Temps");
        foreach ($tempsUrl as $k => $v) {
            //if($k==1) break;			
            preg_match("/id=(\d+)/", $v, $matchid);
            $id = $matchid[1];
            $is_exists = $tempModel->select($id);
            if ($is_exists)
                continue;
            $data[$flag]['id'] = $id;
            $thisContent = file_get_contents($v);
            $thisContent = preg_replace("/\s/", "", $thisContent);
            $thisContent = iconv("gbk", "utf-8", $thisContent);
            if (empty($id)) {
                $this->error("模板地址有误");
            }
            if (strpos($v, "zxn")) {
                $data[$flag]['type'] = 1;
                $data[$flag]['farmwork'] = 1;
                preg_match("/<spanclass=\"price-s-show\">([^<]+)<\/span>/", $thisContent, $pricematch);
            } else {
                $data[$flag]['type'] = 2;
                $data[$flag]['farmwork'] = 6;
                preg_match("/<dt>模板价格：<\/dt><dd><span>[^<]+<\/span><strong>([^<]+)<\/strong>/", $thisContent, $pricematch);
            }
            if ($pricematch[1])
                $data[$flag]['price'] = $data[$flag]['type'] == 1 ? $pricematch[1] / 3 : $pricematch[1];
            else {
                preg_match("/<dt>模板原价：<\/dt><dd><span>[^<]+<\/span><strong>([^<]+)<\/strong>/", $thisContent, $pricematch1);
                $data[$flag]['price'] = $data[$flag]['type'] == 1 ? $pricematch1[1] / 3 : $pricematch1[1];
            }
            preg_match("/<divclass=\"sale-info\"><h2>([^<>]+)<\/h2>/", $thisContent, $titlematch);  //匹配模板标题
            $data[$flag]['title'] = $titlematch[1];
            preg_match("/<dt>总销售量：<\/dt><dd><strong>(\d+)<\/strong>/", $thisContent, $countmatch);  //匹配模板销量					
            preg_match_all("/http:\/\/img[^\"]+\d{3}[x-]\d{3}\.jpg/", $thisContent, $frontpage);
            //dump($frontpage);
            $data[$flag]['frontpage'] = $frontpage[0][0];
            $data[$flag]['userid'] = $_SESSION['userid'];
            $data[$flag]['ww'] = $this->_param("designer");
            $flag++;
        }
        $result = $tempModel->addAll($data);
        if ($result) {
            $this->success("OK");
        }
    }

    public function formwork() {
        $formwork = $this->_param("formwork");
        echo $formwork;
    }
    
    //完善资料
    public function selectid($id = "") {
        $te = M("temps");
        $li = $te->where("id = $id")->select();
        $li = $li[0];
        $html = '<div class="mode">
    <div class="head"><h3>编辑模块信息</h3></div>
    <div class="con">
        <div class="formwork">
            <div class="title">旺铺模板</div>
            <div class="c">
            <select name="formwork" id="formwork">';
        $html .= '<option value="0" ';
        if ($li['farmwork'] == 0) {
            $html .= " selected='selected'";
        }
        $html .='>请选择</option>';
        $html .= '<option value="1" ';
        if ($li['farmwork'] == 1) {
            $html .= " selected='selected'";
        }
        $html .='>高级模板</option>';
        $html .= '<option value="2"';
        if ($li['farmwork'] == 2) {
            $html .= " selected='selected'";
        }
        $html .='>简易模板</option> ';
        $html .= ' <option value="3" ';
        if ($li['farmwork'] == 3) {
            $html .= " selected='selected'";
        }
        $html .='>JS模板</option> ';
        $html .= ' <option value="4" ';
        if ($li['farmwork'] == 4) {
            $html .= " selected='selected'";
        }
        $html .='>TAE模板</option>';
        $html .= ' <option value="6" ';
        if ($li['farmwork'] == 6) {
            $html .= " selected=s'selected'";
        }
        $html .='>高级旧模板</option>';
        $html .= ' <option value="7" ';
        if ($li['farmwork'] == 7) {
            $html .= " selected=s'selected'";
        }
        $html .='>简易旧模板</option>';
        $html .='</select>
            </div>
        </div>
        <div class="formwork">
            <div class="title">行业类型</div>
            <div class="c">
            <select name="trade" id="trade">
            <option value="0" ';
        if ($li['trade'] == 0) {
            $html .= " selected='selected'";
        }
        $html .= '>请选择</option>
              <option value="1" ';
        if ($li['trade'] == 1) {
            $html .= " selected='selected'";
        }
        $html .= '>通用</option>
              <option value="2"';
        if ($li['trade'] == 2) {
            $html .= " selected='selected'";
        }
        $html.='>服装配饰</option>
              <option value="3"';
        if ($li['trade'] == 3) {
            $html .= " selected='selected'";
        }
        $html .='>精品鞋包</option>
              <option value="4"';
        if ($li['trade'] == 4) {
            $html .= " selected='selected'";
        }
        $html .='>化妆健美</option>
              <option value="5"';
        if ($li['trade'] == 5) {
            $html .= " selected='selected'";
        }
        $html .='>饰品珠宝</option>
              <option value="6"';
        if ($li['trade'] == 6) {
            $html .= " selected='selected'";
        }
        $html .='>运动户外</option>
              <option value="7"';
        if ($li['trade'] == 7) {
            $html .= " selected='selected'";
        }
        $html .='>家居装饰</option>
              <option value="8" ';
        if ($li['trade'] == 8) {
            $html .= " selected='selected'";
        }
        $html .= '>数码家电</option>
              <option value="9"';
        if ($li['trade'] == 9) {
            $html .= " selected='selected'";
        }
        $html .= '>母婴用品</option>
              <option value="10"';
        if ($li['trade'] == 10) {
            $html .= " selected='selected'";
        }
        $html.='>食品保健</option>
              <option value="11"';
        if ($li['trade'] == 11) {
            $html .= " selected='selected'";
        }
        $html.='>生活兴趣</option>
              <option value="12"';
        if ($li['trade'] == 12) {
            $html .= " selected='selected'";
        }
        $html.='>虚拟充值</option>
            </select>
            </div>
        </div>
        <div class="formwork">
            <div class="title">色系</div>
            <div class="c">
            <select name="color" id="color">
            <option value="0"';
        if ($li['color'] == 0) {
            $html .= " selected='selected'";
        }
        $html.='>请选择</option>
              <option value="1"';
        if ($li['color'] == 1) {
            $html .= " selected='selected'";
        }
        $html.='>红色</option>
              <option value="2"';
        if ($li['color'] == 2) {
            $html .= " selected='selected'";
        }
        $html .='>蓝色</option>
              <option value="3"';
        if ($li['color'] == 3) {
            $html .= " selected='selected'";
        }
        $html .='>绿色</option>
              <option value="4"';
        if ($li['color'] == 4) {
            $html .= " selected='selected'";
        }
        $html.='>橙色</option>
              <option value="5"';
        if ($li['color'] == 5) {
            $html .= " selected='selected'";
        }
        $html .='>粉色</option>
              <option value="6"';
        if ($li['color'] == 6) {
            $html .= " selected='selected'";
        }
        $html.='>黑色</option>
            <option value="7"';
        if ($li['color'] == 7) {
            $html .= " selected='selected'";
        }
        $html.='>黑白色</option>
            <option value="8"';
        if ($li['color'] == 8) {
            $html .= " selected='selected'";
        }
        $html.='>换彩色</option>
            </select>
            </div>
        </div>
        <div class="formwork">
            <div class="title">风格</div>
            <div class="c">
            <select name="sty" id="sty">
            <option value="0"';
        if ($li['sty'] == 0) {
            $html .= " selected='selected'";
        }
        $html .='>请选择</option>
              <option value="1"';
        if ($li['sty'] == 1) {
            $html .= " selected='selected'";
        }
        $html .='>时尚</option>
              <option value="2"';
        if ($li['sty'] == 2) {
            $html .= " selected='selected'";
        }
        $html.='>简约</option>
              <option value="3"';
        if ($li['sty'] == 3) {
            $html .= " selected='selected'";
        }
        $html.='>古典</option>
              <option value="4"';
        if ($li['sty'] == 4) {
            $html .= " selected='selected'";
        }
        $html.='>非主流</option>
              <option value="5"';
        if ($li['sty'] == 5) {
            $html .= " selected='selected'";
        }
        $html.='>酷炫</option>
              <option value="6" ';
        if ($li['sty'] == 6) {
            $html .= " selected='selected'";
        }
        $html .='>可爱</option>
              <option value="7"';
        if ($li['sty'] == 7) {
            $html .= " selected='selected'";
        }
        $html.='>节日庆典</option>
            </select>
            </div>
        </div>
        <div class="formwork">
            <div class="title">每日限额</div>
            <div class="c">
                <input type="text" size="3" name="num" id="num" value="' . $li['num'] . '" onkeyup="this.value=this.value.replace(/\D/g,' . "''" . ')" /> 套
            </div>
        </div>
		<div class="formwork">
            <div class="title">分成比例</div>
            <div class="c">
			<select name="rate" id="rate">           
			<option value="100" ';
		if ($li['rate'] == 1) {
            $html .= " selected='selected'";
        }
		$html.=' >100%</option>
			<option value="70"';
		if ($li['rate'] == 0.7) {
            $html .= " selected='selected'";
        }
		$html.='>70%</option>
			<option value="50"';
		if ($li['rate'] == 0.5) {
            $html .= " selected='selected'";
        }
		$html.='>50%</option>
			</select>
            </div>
        </div>
        <div class="submit"><input type="button" value="保存" onclick="query(' . $id . ')" /><input type="button" onclick="qbot()" value="取消"/></div>
    </div>
</div>
';

        echo $html;
    }
    //完善资料
    function addsty($id = '', $f = '', $t = '', $c = '', $s = '', $n = '',$r='') {
        $te = M("temps");
        $l = $te->where("id = $id")->getField("status");
        if ($l == 1) {
            if ($f == 0 || $t == 0 || $c == 0 || $s == 0 || $n == 0) {
                echo "0";
            } else {
                $data['farmwork'] = $f;
                $data['trade'] = $t;
                $data['color'] = $c;
                $data['sty'] = $s;
                $data['num'] = $n;               
				$data['rate'] = $r/100;
                $li = $te->where("id = $id")->save($data);
                echo "1";
            }
        } else {
            $data['farmwork'] = $f;
            $data['trade'] = $t;
            $data['color'] = $c;
            $data['sty'] = $s;
            $data['num'] = $n;
			$data['rate'] = $r/100;
            $li = $te->where("id = $id")->save($data);
            if ($li >= 0) {
                echo "1";
            }
        }
    }

    //我的订单
    function orderlist() {
		$this->orderlist1();
        $this->display();
    }


    public function orderlist1() {
        $orderModel=M("Orders");
		$te = M("Temps");
		if($_SESSION['type']==1){		//设计师用户
			$map['fname']=$_SESSION['name'];
			$map['sjssta']=1;
			}
		else{							//掌柜用户
			$map['name']=$_SESSION['name'];
			$map['zgsta']=1;
			}
		$map['status']=array('neq',3);		//获取进行中的订单
		$orders1=$orderModel->where($map)->order("status asc,date desc")->select();
		import('ORG.Util.Page'); // 导入分页类
		$count1=count($orders1);
		$Page1 = new Page($count1, 8);
        $show1 = $Page1->show(); // 分页显示输出
        $list1 = $orderModel->where($map)->order("status asc,date desc")->limit($Page1->firstRow . ',' . $Page1->listRows)->select();
        foreach ($list1 as $key => $l) {
            $name1[$key] = $te->where("id = '" . $l['tempid'] . "'")->select(); //模板信息
            }
		$this->assign("page1",$show1);		//进行中的订单的分页数
		$this->assign("list1",$list1);		//进行中的订单的该页记录
		$this->assign("temps1",$name1);		//进行中的订单的该页记录对应的模板列表
		
		$map['status']=array('eq',3);		//获取进行中的订单
		$orders2=$orderModel->where($map)->order("status asc,date desc")->select();
		import('ORG.Util.Page'); // 导入分页类
		$count2=count($orders2);
		$Page2 = new Page($count2, 8);
        $show2 = $Page2->show(); // 分页显示输出
        $list2 = $orderModel->where($map)->order("status asc,date desc")->limit($Page2->firstRow . ',' . $Page2->listRows)->select();
        foreach ($list2 as $key => $l) {
            $name2[$key] = $te->where("id = '" . $l['tempid'] . "'")->select(); //模板信息
            }
		$this->assign("page2",$show2);		//进行中的订单的分页数
		$this->assign("list2",$list2);		//进行中的订单的该页记录
		$this->assign("temps2",$name2);		//进行中的订单的该页记录对应的模板列表
    }

    //用户确认
    public function allmes() {
        $ind = $this->_param("ind"); //订单id
        echo '<div id="g" class="tg" ><div class="ce"><h3>是否完成购买</h3><div class="in">请输入购买的旺旺账号:<input type="text" id="wwname" /></div><div class="tx"><a href="javascript:;" onclick="wsy(1,' . $ind . ')" >确认</a><a href="javascript:;" onclick="wsy(0)">取消</a></div></div></div>';
    }

    public function allmesy() {
        $ind = $this->_param("ind");
        $wwname = $this->_param("wwname");
        $order = M("orders");
        $user = M("users");
        $data['status'] = 2;
        $data['wwname'] = $wwname;
        $order->where("indent = $ind")->save($data);

        $fname = $order->where("indent = $ind")->getField("fname"); //获取设计师用户名
        //掌柜购买确认 通知设计师
        //短信通知，上线时启用
//        $phone = $user->where("name = '" . $fname . "'")->getField("phone"); //获取设计师手机号码
//        $name = "wxr";
//        $pwd = "wxr19890203";
//        $msg = rawurlencode("用户：".$_SESSION['name'] . ",旺旺名：".$wwname."已够买您的模板,请登录:http://www.fanli.me/index.php/Public/login 我的订单进行查看");
//        $res = file_get_contents("http://www.gysoft.cn/smspost_utf8/send.aspx?username={$name}&password={$pwd}&mobile={$phone}&content={$msg}");
    }

    public function delorder($id) {
        $id = $this->_param("id");
        $tempModel = M("orders");
        if (!is_array($id))
            $id = array($id);
        $map['id'] = array('in', $id);
        $result = $tempModel->where($map)->delete();
        if ($result) {
            $this->redirect("/User/orderlist", 0);
        }
    }

    //设计师确认
    public function que() {
        $price = $this->_param("price"); //返利费用
        $zgname = $this->_param("zgname"); //获得返利用户
        $ind = $this->_param("ind"); //订单
        $id = $this->_param("id"); //模板id
        $temps = M("temps");
        $temp = M("orders");
        $ye = M("users");
        $testy = $temps->where("id = $id")->getField("type"); //是否新旧模板
        $title = $temps->where("id = $id")->getField("title"); //标题
        if ($testy == 1) {
            $price = $price * 3; //新模板 费用x3
        }
        $li = $ye->where("name ='" . $_SESSION['name'] . "'")->getField("money"); //查询登录用户账户余额
        if ($_GET) {
            if ($li >= $price) {//如果账户余额足够支付本次返利 
                $bill = M("bill");
                $r = rand("100", "999");
                $data['status'] = 3;
                $temp->where("indent = $ind")->save($data); //更新订单信息
                $ye->where("name= '" . $_SESSION['name'] . "'")->setDec('money', $price); //付款
                $ye->where("name = '" . $zgname . "'")->setInc('money', $price); //收款
                $data1['name'] = $_SESSION['name'];
                $data1['forname'] = $zgname;
                $data1['indent'] = $ind;
                $data1['tempid'] = $id;
                $data1['money'] = $price;
                $data1['ftime'] = date("Y-m-d H:i:s");
                $data1['title'] = $title;
                $bill->add($data1);
				$temps->where("id = $id")->setInc("count"); //总销量加1
                $temps->where("id = $id")->setInc("todaysale"); //总销量加1               
//                $temps->where("id = $id")->setInc("count1");
//                //设计师确认短信通知掌柜
//                //短信通知，上线时启用
//                $phone = $ye->where("name = '" . $zgname . "'")->getField("phone"); //获取设计师手机号码
//                $name = "wxr";
//                $pwd = "wxr19890203";
//                $msg = rawurlencode("设计师：".$_SESSION['name'] . "已确认您购买的模板,请登录:http://www.fanli.me/index.php/Public/login 我的订单进行查看");
//                $res = file_get_contents("http://www.gysoft.cn/smspost_utf8/send.aspx?username={$name}&password={$pwd}&mobile={$phone}&content={$msg}");
            } else {
                echo "<span>账户余额不足，请您及时 <a href='" . __URL__ . "/chongzhi' style='color:red;'>充值</a></span>";
            }
        }
    }

    //删除所提交订单
    function dellorder() {
        $id = $this->_param("id"); //删除id
        $orders = M("orders");
        if ($_SESSION['type'] == 1) {
            $data['sjssta'] = 0;
        } elseif ($_SESSION['type'] == 2) {
            $data['zgsta'] = 0;
        }
        $li = $orders->where("id = $id")->save($data);

        //双方删除，删除数据
        $s = $orders->where("id = $id")->getField("sjssta");
        $z = $orders->where("id = $id")->getField("zgsta");
        if ($s == 0 and $z == 0) {
            $orders->where("id = $id")->delete();
        }
        if ($li) {
            echo "1";
        }
    }

    //账单明细
    function zdmx() {
        import('ORG.Util.Page'); // 导入分页类
        if ($_SESSION['type'] == 1) {
            $t = 1;
        } else {
            $t = 2;
        }
        $in = $this->_param("in") ? $this->_param("in") : $t;
        $this->assign("in", $in);
        if ($t == 1) {
            if ($in == 1) {
                $where = "name = '" . $_SESSION['name'] . "' and sjssta = 1"; //我的支出(设计师)
            } else {
                $where = "forname = '" . $_SESSION['name'] . "' and sjssta = 1"; //我的收入(设计师)
            }
        } else {
            if ($in == 1) {
                $where = "name = '" . $_SESSION['name'] . "' and zgsta = 1"; //我的支出(掌柜)
            } else {
                $where = "forname = '" . $_SESSION['name'] . "' and zgsta = 1"; //我的收入(掌柜)
            }
        }
        $b = M("bill");
        $temp = M("temps");
        $count = $b->where($where)->order("ftime desc")->count(); // 查询满足要求的总记录数
        $Page = new Page($count, 5);
        $show = $Page->show(); // 分页显示输出
        $list1 = $b->where($where)->order("ftime desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list1 as $key => $li) {
            $type[$key] = $temp->where("id = " . $li['tempid'])->getField("type"); //获得模板新旧类型
        }
        $this->assign('page', $show); // 赋值分页输出
        $this->assign("type", $type);
        $this->assign("list1", $list1);
        $this->display();
    }

    //删除账单
    function delbill() {
        $id = $this->_param("id");
        $b = M("bill");
        if ($_SESSION['type'] == 1) {
            $data['sjssta'] = 0;
        } elseif ($_SESSION['type'] == 2) {
            $data['zgsta'] = 0;
        }
        $li = $b->where("id = $id")->save($data);

        //双方删除，删除数据
        $s = $b->where("id = $id")->getField("sjssta");
        $z = $b->where("id = $id")->getField("zgsta");
        if ($s == 0 and $z == 0) {
            $b->where("id = $id")->delete();
        }
        if ($li) {
            echo "1";
        }
    }
	
	function changephone(){		
		$this->display();
		}
	
	function changephone_do(){
		$newphone=$this->_param("new_phone");
		$code=$this->_param("code");
		$pwd=$this->_param("pwd");
		$userModel=M("Users");
		$user=$userModel->select($_SESSION['userid']);
		//var_dump($_SESSION);
		if(md5($pwd)!=$user[0]['pwd']) $this->error("登录密码错误");
		elseif($code != $_SESSION['sms_verify']) $this->error("手机验证码错误");
		$_SESSION['phone']=$newphone;
		$data['id']=$_SESSION['userid'];
		$data['phone']=$newphone;
		$result=$userModel->save($data);
		if($result) $this->success("成功");	
		}	

}

?>